using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace AfterEdge.Web.UI
{
    public class PromptButtonCollection : Collection<PromptButton> { }

    public class PromptButton : WebControl
    {
        private String _text;
        private String _commandArgument;
        private bool _autoPostBack;
        private String _onClickClick;
        private Prompt _prompt;

        public PromptButton() { }

        internal PromptButton(String id, String commandArgument, String text, bool autoPostBack)
        {
            this.ID = id;
            this._commandArgument = commandArgument;
            this._text = text;
            this._autoPostBack = autoPostBack;
        }

        /// <summary>
        /// Gets or sets static text of the button within the prompt.
        /// </summary>
        /// <value>The text.</value>
        [PersistenceMode(PersistenceMode.Attribute)]
        [Category("Data")]
        public String Text
        {
            get { return this._text; }
            set { this._text = value; }
        }

        /// <summary>
        /// Gets or sets the command argument of the button when causing a postback to the server.
        /// This is commonly used within the Command Event of the prompt control.
        /// </summary>
        /// <value>The command argument.</value>
        [PersistenceMode(PersistenceMode.Attribute)]
        [Category("Behavior")]
        public String CommandArgument
        {
            get { return this._commandArgument; }
            set { this._commandArgument = value; }
        }

        /// <summary>
        /// Gets or sets the client side script that will execute when this button is clicked.
        /// </summary>
        /// <value>The on client click.</value>
        [PersistenceMode(PersistenceMode.Attribute)]
        [Category("Behavior")]
        public String OnClientClick
        {
            get { return this._onClickClick; }
            set { this._onClickClick = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this button raises a page postback or not.
        /// </summary>
        /// <value><c>true</c> if [auto post back]; otherwise, <c>false</c>.</value>
        [PersistenceMode(PersistenceMode.Attribute)]
        [Category("Behavior")]
        public bool AutoPostBack
        {
            get { return this._autoPostBack; }
            set { this._autoPostBack = value; }
        }

        /// <summary>
        /// Gets the parent prompt container of this button.
        /// </summary>
        /// <value>The prompt.</value>
        [Browsable(false)]
        public Prompt Prompt
        {
            get { return this._prompt; }
        }

        internal void SetPrompt(Prompt prompt)
        {
            this._prompt = prompt;
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Input; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            this.Attributes.Add("type", "button");
            this.Attributes.Add("value", this.Text);

            base.AddAttributesToRender(writer);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this.Parent.GetType() != typeof(Prompt))
            {
                throw new InvalidOperationException("PromptButton must be placed inside of a Prompt Control.");
            }
        }
    }
}
